package com.xinyue.game.service.framwork.context;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.xinyue.game.service.framwork.common.GameFrameworkConfig;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.time.Duration;

public class GameUserChannelCacheManager {

    private Cache<String, GameUserChannel> gameUserChannelMap;
    @Autowired
    private GameFrameworkConfig gameFrameworkConfig;

    @PostConstruct
    public void init() {
        gameUserChannelMap = CacheBuilder.newBuilder().initialCapacity(1000).maximumSize(gameFrameworkConfig.getUserChannelCacheMaxSize())
                .expireAfterAccess(Duration.ofMinutes(gameFrameworkConfig.getUserChannelExpire())).build();
    }

    public void addCache(String userKey, GameUserChannel userChannel) {
        gameUserChannelMap.put(userKey, userChannel);
    }

    public GameUserChannel getGameUserChannel(String userKey) {
        return this.gameUserChannelMap.getIfPresent(userKey);
    }
}
